<?php
error_reporting( E_ALL | E_STRICT );
//include_once "rank.php";
include_once "timeRank.php";
$redis = new Redis();
$redis->connect("127.0.0.1", 6379);

//简单的排行榜
$data = [
    ['uid' => 1, 'score' => 100, 'time' => time()],
    ['uid' => 2, 'score' => 200, 'time' => time()],
    ['uid' => 3, 'score' => 300, 'time' => time()],
    ['uid' => 4, 'score' => 400, 'time' => time()],
    ['uid' => 5, 'score' => 500, 'time' => time()],
    ['uid' => 6, 'score' => 600, 'time' => time()],];

$key = 'rank2024-08-24';

foreach ($data as $v) {
    $redis->zAdd($key, $v['score'], $v['uid']);
}

/**
 * 普通排行榜
 */
/*$rank = new Rank(['key'=> 'rank:2024-10-17', 'expire' => '86400']);
$rank->generate(120);
$rank->set('liming', 800, false);
$rank->incr('liming', 10);
$list = $rank->getRankList();
$count = $rank->count();
var_dump($count, $list);*/

/**
 * 二进制位整数的时间戳排行榜
 */
/*echo "\n";
$rt = [];
$rank = new BitRank(['key'=> 'rank.bitRank:2024-10-17', 'expire' => '86400']);
//$rank->generate(120);
$rt[] = $rank->set('liming', 700,false);
//$rt[] = $rank->set('liming', 15,true);
//最大分数 2**20-1 超过 依然能够存储,但是时间部分失精
$rt[] = $rank->set('maxscore', 2**20-1,false);
$rt[] = $rank->set('over', 2**20,false); //9007207524544320
$time  = time();
//同分情况 2**20 同分失去精度, over2 比 over1
$rt[] = $rank->set('over1', 2**20,false, $time); //9007207524544320
$rt[] = $rank->set('over2', 2**20,false, $time+1); //9007198934609729
$list = $rank->getRankList(10);
$count = $rank->count();
var_dump($rt, $count, $list);*/

/**
 * 整数的时间戳排行榜
 */
/*$rt = [];
$rank = new DecTimeRank(['key'=> 'rank.DecTimeRank:2024-10-17', 'expire' => '86400']);

//同名和自增 检测
$rt[] = $rank->set('waner', 700,false, time()+15);
$rt[] = $rank->set('lisi', 700,false);
$rt[] = $rank->set('ximing', 700,false);
$rt[] = $rank->set('ximing', 15,true, time()+10);

$rt[] = $rank->set('wangwu', 700,false, time()+10);

//最大分数 900719 超过900719可以存储,但是时间部分会丢失精度,分数部分在高位,精度从低位开始失去,所以分数还是可以取到的
$rt[] = $rank->set('max', 900719,false);
$rt[] = $rank->set('over', 900720,false);
$list = $rank->getRankList(10);
$count = $rank->count();
var_dump($rt, $count, $list);*/


/**
 * 浮点数时间戳排行榜
 */
$rt = [];
$rank = new TimeRank(['key'=> 'rank.TimeRank:2024-10-30', 'expire' => '86400']);

//同名和自增 检测
$rt[] = $rank->set('waner', 700,false, time()+15);
$rt[] = $rank->set('lisi', 700,false);
$rt[] = $rank->set('wangwu', 700,false, time()+10);
$rt[] = $rank->set('ximing', 700,false);
$rt[] = $rank->set('ximing', 15,true, time()+10);


//最大分数 2**20-1 超过 依然能够存储,但是时间部分失精
//$rt[] = $rank->set('max', 2**20-1,false);
//$rt[] = $rank->set('over', 2**20,false);
$list = $rank->getRankList(10);
$count = $rank->count();
var_dump($rt, $count, $list);


/**
 *  双字段排序
 */
$rt = [];
$rank = new MulitRank(['key'=> 'rank.MulitRank:2024-10-31', 'expire' => '86400']);

//同名和自增 检测
$rt[] = $rank->set('waner', 700,false, time()+15);
$rt[] = $rank->set('lisi', 700,false);
$rt[] = $rank->set('wangwu', 700,false, time()+10);
$rt[] = $rank->set('ximing', 700,false);
$rt[] = $rank->set('ximing', 15,true, time()+10);
$rt[] = $rank->set('hanmeimei', 700,false, time()+20);
$list = $rank->getRankList(10);
$count = $rank->count();
$rt[] = $rank->getInfo('hanmeimei');
var_dump($rt, $count, $list);

